<?php

/**
 * This is the model class for table "meinian_meinian_questionnaireuestionnaire".
 *
 * The followings are the available columns in table 'meinian_questionnaire':
 * @property integer $id
 * @property string $title
 * @property string $type
 */
class EvaluateQuestionnaire extends CActiveRecord {

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return EvaluateQuestionnaire the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'evaluate_questionnaire';
	}

	/**
	 *
	 * Enter description here ...
	 * @param unknown_type $uid
	 * @param unknown_type $hum_tags_arr
	 */
	public function saveHumanTags($uid, $hum_tags_arr) {
		$hum_ret = EvaluateHumanHealthTag::model()->findByAttributes(array("uid" => $uid));
		if (!empty($hum_ret)) {
			if (empty($hum_ret->badtags)) {
				$str = implode(",", $hum_tags_arr);
				$hum_ret->badtags = $str;
			} else {
				foreach ($hum_tags_arr as $key => $value) {
					if (strpos($hum_ret->badtags, $value) === false) {
						$hum_ret->badtags .= "," . $value;
					}
				}
			}

			$hum_ret->save();
		} else {
			$hum_ins = new EvaluateHumanHealthTag();
			$hum_ins->uid = $uid;
			$str = implode(",", $hum_tags_arr);
			$hum_ins->badtags = $str;
			//			var_dump($hum_ins);exit();
			$hum_ins->save();
		}
	}

	/**
	 *
	 * Enter description here ...
	 * @param unknown_type $user the use has use
	 * @param unknown_type $q array of all the result form has had q['page_id']['ans']['element']
	 */
	public function saveQAnswer($uid, $q) {
		//        $user = EvaluateUserDiselement::model()->findByAttributes(
		//                    array("uid"=>$uid,),array("order"=>"id desc")
		//                );
		//        if($user === null){
		//            $user = new EvaluateUserDiselement();
		//            $user->uid = $uid;
		//            try {
		//                if(false === $user->save()){
		//                    throw new Exception("create new EvaluateUserDiselement item error");
		//                }
		//            } catch (Exception $exc) {
		//                return $exc->getMessage();
		//            }
		//        }
		$items = array();
		try {

			foreach ($q as $key => $value) {
				$fc = "saveQA_" . $key;
				$value['ans'] = isset($value['ans']) ? $value['ans'] : array();
				if(method_exists($this, $fc)){
					$ret = $this->$fc($uid, $value["ans"]);
					if($ret !== null){
						$items = array_merge($items, $ret);
					}
				}
			}

			if(false === B2bUserQuestionResult::model()->saveResult($uid,$items)){
				return false;
				//                throw new Exception("update $uid EvaluateUserDiselement item error");
			}
			return $items;
		} catch (Exception $exc) {
			return false;
			//            throw new Exception($exc->getMessage());
		}
	}
	public function convertQAnswer($uid, $q) {

		$items = array();
		try {

			foreach ($q as $key => $value) {
				$fc = "saveQA_" . $key;
				$value['ans'] = isset($value['ans']) ? $value['ans'] : array();
				if(method_exists($this, $fc)){
					$ret = $this->$fc($uid, $value["ans"]);
					if($ret !== null){
						$items = array_merge($items, $ret);
					}
				}
			}
			return $items;
		} catch (Exception $exc) {
			return false;
		}
	}


	//    public function saveQA_112($uid, $q) {
	//        $anserw_array = array();
	//        if ($q['teacher'] == 'yes') {
	//            array_push($anserw_array, '肩膀');
	//        }
	//        if ($q['driver'] == 'yes') {
	//            array_push($anserw_array, '腰');
	//        }
	//        if ($q['whitecollar'] == 'yes') {
	//            array_push($anserw_array, '颈椎');
	//        }
	//        $anserw_str = implode(",", $anserw_array);
	////        $user->badtags .= $anserw_str;
	//    }
	//
	//    private function saveQA_111($uid, $q) {
	//
	//        $anserw_array = array();
	//        if (($q['watchtelephone'] == 'yes') || ($q['watchingontrain'] == 'yes') || ($q['watchinbed'] == 'yes')) {
	//            array_push($anserw_array, '眼睛');
	//        }
	//
	//        $anserw_str = implode(",", $anserw_array);
	////        $user->badtags .= $anserw_str;
	//    }

	private function saveQA_1($uid, $q) {

		$item = null;
		if (isset($q["year"], $q["month"], $q["day"])) {
			$item["age"] = $q["year"] . "-" . $q["month"] . "-" . $q["day"];
		}

		return $item;
	}

	private function saveQA_2($uid, $q) {
		$item = null;
		if (isset($q["gender"]) && in_array($q["gender"], array(JSONHelper::MAN, JSONHelper::WOMAN))) {
			$item["gender"] = $q["gender"];
		}
		return $item;
	}

	private function saveQA_3($uid, $q) {
		$item = null;
		if (!empty($q["hypertension_family_history"])) {
			$item['hypertension_family_history'] = 1;
		} else {
			$item['hypertension_family_history'] = 0;
		}
		if (!empty($q["coronary_family_history"])) {
			$item['coronary_family_history'] = 1;
		} else {
			$item['coronary_family_history'] = 0;
		}
		if (!empty($q["stroke_family_history"])) {
			$item['stroke_family_history'] = 1;
		} else {
			$item['stroke_family_history'] = 0;
		}
		if (!empty($q["diabetes_family_history"])) {
			$item['diabetes_family_history_single'] = 1;
			$item['diabetes_family_history_double'] = 1;
			//            if ($q["diabetes_family_history"] == "diabetes_family_history_single") {
			//                $user->diabetes_family_history_single = 1;
			//                $user->diabetes_family_history_double = 0;
			//            } elseif ($q["diabetes_family_history"] == "diabetes_family_history_double") {
			//                $user->diabetes_family_history_single = 1;
			//                $user->diabetes_family_history_double = 1;
			//            }
		} else {
			$item['diabetes_family_history_single'] = 0;
			$item['diabetes_family_history_double'] = 0;
		}
		//胃癌
		if (isset($q["weiai_family_history"]) && $q["weiai_family_history"] == 1) {
			$item['weiai_family_history'] = 1;
		} else {
			$item['weiai_family_history'] = 0;
		}
		//肝癌
		if (isset($q["ganai_family_history"]) && $q["ganai_family_history"] == 1) {
			$item['ganai_family_history'] = 1;
		} else {
			$item['ganai_family_history'] = 0;
		}
		//肺癌
		if (isset($q["feiai_family_history"]) && $q["feiai_family_history"] == 1) {
			$item['feiai_family_history'] = 1;
		} else {
			$item['feiai_family_history'] = 0;
		}
		//肿瘤
		if (isset($q["zhongliu_family_history"]) && $q["zhongliu_family_history"] == 1) {
			$item['zhongliu_family_history'] = 1;
		} else {
			$item['zhongliu_family_history'] = 0;
		}
		//直肠癌
		if (isset($q["zhichangai_family_history"]) && $q["zhichangai_family_history"] == 1) {
			$item['zhichangai_family_history'] = 1;
		} else {
			$item['zhichangai_family_history'] = 0;
		}
		//食管癌
		if (isset($q["shiguanai_family_history"]) && $q["shiguanai_family_history"] == 1) {
			$item['shiguanai_family_history'] = 1;
		} else {
			$item['shiguanai_family_history'] = 0;
		}
		//鼻咽癌
		if (isset($q["biyanai_family_history"]) && $q["biyanai_family_history"] == 1) {
			$item['biyanai_family_history'] = 1;
		} else {
			$item['biyanai_family_history'] = 0;
		}
		//胰腺癌
		if (isset($q["yixianai_family_history"]) && $q["yixianai_family_history"] == 1) {
			$item['yixianai_family_history'] = 1;
		} else {
			$item['yixianai_family_history'] = 0;
		}
		//消化道癌
		if (isset($q["xiaohuadaoai_family_history"]) && $q["xiaohuadaoai_family_history"] == 1) {
			$item['xiaohuadaoai_family_history'] = 1;
		} else {
			$item['xiaohuadaoai_family_history'] = 0;
		}
		//膀胱癌
		if (isset($q["pangguangai_family_history"]) && $q["pangguangai_family_history"] == 1) {
			$item['pangguangai_family_history'] = 1;
		} else {
			$item['pangguangai_family_history'] = 0;
		}
		return $item;
	}

	private function saveQA_4($uid, $q) {
		$item = null;
		$hum_tags_arr = array();
		if (isset($q["hypertension_history"]) && $q["hypertension_history"] == 1) {
			$item['hypertension_history'] = 1;
			array_push($hum_tags_arr, "高血压");
		} else {
			$item['hypertension_history'] = 0;
		}

		if (isset($q["coronary_history"]) && $q["coronary_history"] == 1) {
			$item['coronary_history'] = 1;
			array_push($hum_tags_arr, "冠心病");
		} else {
			$item['coronary_history'] = 0;
		}


		if (isset($q["diabetes_history"]) && $q["diabetes_history"] == 1) {
			$item['diabetes_history'] = 1;
			array_push($hum_tags_arr, "糖尿病");
		} else {
			$item['diabetes_history'] = 0;
		}

		if (isset($q["hyperlipemia_history"]) && $q["hyperlipemia_history"] == 1) {
			$item['hyperlipemia_history'] = 1;
			array_push($hum_tags_arr, "高血脂");
		} else {
			$item['hyperlipemia_history'] = 0;
		}

		if (isset($q["tuberculosis_history"]) && $q["tuberculosis_history"] == 1) {
			$item['tuberculosis_history'] = 1;
			array_push($hum_tags_arr, "肺结核");
		} else {
			$item['tuberculosis_history'] = 0;
		}

		if (isset($q["pneumonia_history"]) && $q["pneumonia_history"] == 1) {
			$item['pneumonia_history'] = 1;
			array_push($hum_tags_arr, "肺炎");
		} else {
			$item['pneumonia_history'] = 0;
		}

		if (isset($q["helicobacterpylori_history"]) && $q["helicobacterpylori_history"] == 1) {
			$item['helicobacterpylori_history'] = 1;
			array_push($hum_tags_arr, "胃溃疡#消化道溃疡");
		} else {
			$item['helicobacterpylori_history'] = 0;
		}

		if (isset($q["hepatitisB_history"]) && $q["hepatitisB_history"] == 1) {
			$item['hepatitisB_history'] = 1;
			array_push($hum_tags_arr, "乙肝");
		} else {
			$item['hepatitisB_history'] = 0;
		}

		if (isset($q["hepatitisC_history"]) && $q["hepatitisC_history"] == 1) {
			$item['hepatitisC_history'] = 1;
			array_push($hum_tags_arr, "丙肝");
		} else {
			$item['hepatitisC_history'] = 0;
		}

		if (isset($q["changxirou_history"]) && $q["changxirou_history"] == 1) {
			$item['changxirou_history'] = 1;
			array_push($hum_tags_arr, "肠息肉");
		} else {
			$item['changxirou_history'] = 0;
		}

		if (isset($q["manxingchangyan_history"]) && $q["manxingchangyan_history"] == 1) {
			$item['manxingchangyan_history'] = 1;
			array_push($hum_tags_arr, "慢性肠炎");
		} else {
			$item['manxingchangyan_history'] = 0;
		}

		if (isset($q["bianmi_history"]) && $q["bianmi_history"] == 1) {
			$item['bianmi_history'] = 1;
			array_push($hum_tags_arr, "便秘");
		} else {
			$item['bianmi_history'] = 0;
		}

		if (isset($q["manxinglanweiyan_history"]) && $q["manxinglanweiyan_history"] == 1) {
			$item['manxinglanweiyan_history'] = 1;
			array_push($hum_tags_arr, "慢性阑尾炎");
		} else {
			$item['manxinglanweiyan_history'] = 0;
		}

		if (isset($q["ruxianbingshi_history"]) && $q["ruxianbingshi_history"] == 1) {
			$item['ruxianbingshi_history'] = 1;
			array_push($hum_tags_arr, "乳腺病史");
		} else {
			$item['ruxianbingshi_history'] = 0;
		}

		if (isset($q["biyanbingshi_history"]) && $q["biyanbingshi_history"] == 1) {
			$item['biyanbingshi_history'] = 1;
			array_push($hum_tags_arr, "鼻咽病史");
		} else {
			$item['biyanbingshi_history'] = 0;
		}

		if (isset($q["manxingzhiqiguanyan_history"]) && $q["manxingzhiqiguanyan_history"] == 1) {
			$item['manxingzhiqiguanyan_history'] = 1;
			array_push($hum_tags_arr, "慢性支气管炎");
		} else {
			$item['manxingzhiqiguanyan_history'] = 0;
		}

		//        $this->saveHumanTags($user->uid, $hum_tags_arr);
		return $item;
	}

	private function saveQA_5($uid, $q) {
		$item = null;
		if (isset($q["wc"]) && $q["wc"] != 0) {
			$item['wc'] = $q["wc"];
		}
		return $item;
	}

	//吸烟
	private function saveQA_6($uid, $q) {
		$item = null;
		if (isset($q["smoking"])) {
			if ($q["smoking"] == 1) {
				$item['smoking'] = 1;
				$item['smoked'] = 0;
			} else {
				$item['smoking'] = 0;
				if (isset($q["smoked"])) {
					if ($q["smoked"] == 1) {
						$item['smoked'] = 1;
					} else {
						$item['smoked'] = 0;
					}
				}
			}
		}
		return $item;
	}

	private function saveQA_7($uid, $q) {
		$item = null;
		if (isset($q["smoked"])) {
			if ($q["smoked"] == 1) {
				$item['smoked'] = 1;
			} else {
				$item['smoked'] = 0;
			}
		}
		return $item;
	}
	//服用避孕药
	private function saveQA_8($uid, $q) {
		$item = null;
		if (isset($q["contraceptive"])) {
			if ($q["contraceptive"] == 1) {
				$item['contraceptive'] = 1;
			} else {
				$item['contraceptive'] = 0;
			}
		}
		return $item;
	}

	private function saveQA_9($uid, $q) {
		$item = null;
		if (isset($q["hulu"])) {
			if ($q["hulu"] == 1) {
				$item['hulu'] = 1;
			} else {
				$item['hulu'] = 0;
			}
		}
		return $item;
	}

	private function saveQA_10($uid, $q) {
		$item = null;
		if (isset($q["sports"])) {
			if ($q["sports"] == 1) {
				$item['sports'] = 1;
			} else {
				$item['sports'] = 0;
			}
		}
		return $item;
	}

	private function saveQA_11($uid, $q) {
		$item = null;
		if (isset($q["stillsit"])) {
			if ($q["stillsit"] == 1) {
				$item['stillsit'] = 1;
			} else {
				$item['stillsit'] = 0;
			}
		}
		return $item;
	}
	//饮酒
	private function saveQA_12($uid, $q) {
		$item = null;
		if (isset($q["drink"])) {
			if ($q["drink"] == 1) {
				$item['drink'] = 1;
			} else {
				$item['drink'] = 0;
			}
		}
		return $item;
	}

	private function saveQA_13($uid, $q) {
		$item = null;
		if (isset($q["fruit"])) {
			if ($q["fruit"] == 1) {
				$item['fruit'] = 1;
			} else {
				$item['fruit'] = 0;
			}
		}
		return $item;
	}

	private function saveQA_14($uid, $q) {
		$item = null;
		if (isset($q["meat"])) {
			if ($q["meat"] == 1) {
				$item['meat'] = 1;
			} else {
				$item['meat'] = 0;
			}
		}
		return $item;
	}

	private function saveQA_15($uid, $q) {
		$item = null;
		if (isset($q["salt_food"])) {
			if ($q["salt_food"] == "1") {
				$item['salt_food'] = 1;
			} else {
				$item['salt_food'] = 0;
			}
		}
		if (isset($q["hot_food"])) {
			if ($q["hot_food"] == "1") {
				$item['hot_food'] = 1;
			} else {
				$item['hot_food'] = 0;
			}
		}
		if (isset($q["fry_food"])) {
			if ($q["fry_food"] == "1") {
				$item['fry_food'] = 1;
			} else {
				$item['fry_food'] = 0;
			}
		}
		return $item;
	}


	private function saveQA_16($uid, $q) {
		$item = null;
		$hum_tags_arr = array();
		if (!empty($q['height']) && !empty($q['weight'])) {
			$item['height'] = $q["height"];
			$item['weight'] = $q["weight"];
			$item['weightLevel'] = $q["weight"] / (($q["height"] / 100) * ($q["height"] / 100));
			$item['weightLevel'] = round($item['weightLevel'], 2);
			if ($item['weightLevel'] > 24) {
				array_push($hum_tags_arr, "肥胖");
				//                $this->saveHumanTags($user->uid, $hum_tags_arr);
			}
		}
		return $item;
	}

	private function saveQA_17($uid, $q){
		if (isset($q["environment_shimian"]) && $q["environment_shimian"] == 1) {
			$item['environment_shimian'] = 1;
		} else {
			$item['environment_shimian'] = 0;
		}
		if (isset($q["environment_guijiao"]) && $q["environment_guijiao"] == 1) {
			$item['environment_guijiao'] = 1;
		} else {
			$item['environment_guijiao'] = 0;
		}
		if (isset($q["environment_chengshiwuran"]) && $q["environment_chengshiwuran"] == 1) {
			$item['environment_chengshiwuran'] = 1;
		} else {
			$item['environment_chengshiwuran'] = 0;
		}
		if (isset($q["environment_yinshuiwuran"]) && $q["environment_yinshuiwuran"] == 1) {
			$item['environment_yinshuiwuran'] = 1;
		} else {
			$item['environment_yinshuiwuran'] = 0;
		}
		if (isset($q["environment_jushiyouyan"]) && $q["environment_jushiyouyan"] == 1) {
			$item['environment_jushiyouyan'] = 1;
		} else {
			$item['environment_jushiyouyan'] = 0;
		}
		return $item;
	}




	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'option' => array(self::HAS_MANY, 'MeinianQuestionOptions', array('question_id' => 'id')),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
            'id' => 'ID',
            'title' => 'Title',
            'type' => 'Type',
            'questype' => 'Questype',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id);
		$criteria->compare('title', $this->title, true);
		$criteria->compare('type', $this->type, true);
		$criteria->compare('questype', $this->questype, true);

		return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
		));
	}

	public function getOptions($qid) {
		$criteria = new CDbCriteria;
		//		/$criteria->select = 't.title,t.type,t.id,option.name,option.description';
		$criteria->compare('t.id', $qid);
		$criteria->with = 'option';

		$result = EvaluateQuestionnaire::model()->find($criteria);

		return $result;
	}

	public function getQuesinfo($qid_str) {
		$criteria = new CDbCriteria();
		$criteria->addInCondition("id", $qid_str);
        $criteria->order = "questype";

        return EvaluateQuestionnaire::model()->findAll($criteria);
    }

}
